package org.lboot.jpa.utils;

import org.lboot.core.domain.PageParamDTO;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * @author kindear
 */
public class JpaDataUtil{
    /**
     * 构建分页参数
     * @param params
     * @return
     */
    public static Pageable buildPageable(PageParamDTO params){
        Sort sort;
        if(params.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        return PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
    }
}
